//
// Created by Zhangwj on 2023/9/27.
//

#include "IfStmtItemNode.h"

std::unique_ptr<IfStmtItemNode> IfStmtItemNode::IfStmtItemParser(Parser &parser) {
    // 'if' '(' Cond ')' Stmt [ 'else' Stmt ]
    std::unique_ptr<IfStmtItemNode> ifStmtItemNode = std::make_unique<IfStmtItemNode>();
    if (parser.getLexType(0) == LexType::IFTK) {
        parser.next();
    }
    if (parser.getLexType(0) == LexType::LPARENT) {
        parser.next();
    }
    ifStmtItemNode->Cond = CondNode::CondParser(parser);
    if (parser.getLexType(0) == LexType::RPARENT) {
        parser.next();
    }
    ifStmtItemNode->Stmt = StmtNode::StmtParser(parser);
    if (parser.getLexType(0) == LexType::ELSETK) {
        // [ 'else' Stmt ]存在
        parser.next();
        ifStmtItemNode->ElseStmt = StmtNode::StmtParser(parser);
    }
    return ifStmtItemNode;
}
